/*
 * INSANE - Interactive Structural Analysis Environment
 *
 * Copyright (C) 2003-2005
 * Universidade Federal de Minas Gerais
 * Escola de Engenharia
 * Departamento de Engenharia de Estruturas
 * 
 * Author's email :    insane@dees.ufmg.br
 * Author's website :  http://www.dees.ufmg.br/insane
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
package br.ufmg.dees.insane.materialMedia.degeneration;

import java.util.HashMap;
import java.util.ListIterator;

import br.ufmg.dees.insane.materialMedia.constitutiveModel.OnePointConstModel;
import br.ufmg.dees.insane.materialMedia.materialPoint.MaterialPoint;
import br.ufmg.dees.insane.util.IMatrix;
import br.ufmg.dees.insane.util.IVector;

/** A class representing CrossSectionConstMod
 * @author CENOURA!!!
 * @since 31/05/2005
 */
public class CrossSection extends Degeneration {

    private static final long serialVersionUID = 1L;

    /** The generalized strain of the CrossSection.*/
    public final String GENERALIZED_STRAINS = "GENERALIZED_STRAINS";

    /* (non-Javadoc)
     * @see br.ufmg.dees.insane.materialMedia.degeneration.Degeneration#mountC()
     */
    public IMatrix mountC() {
	IMatrix aux = new IMatrix(this.representation.getAnalysisModel().getNumberOfDualInternalVariables(),this.representation.getAnalysisModel().getNumberOfInternalVariables());
	aux.setZero();
	ListIterator mps = this.getMaterialPoints().listIterator();
	while(mps.hasNext()) {
	    MaterialPoint mpt = (MaterialPoint) mps.next();
	    OnePointConstModel opcm = (OnePointConstModel)mpt.getCm();
	    mpt.setConstitutiveVariables(opcm.COORDS, mpt.getPoint());
	    mpt.setConstitutiveVariables(opcm.STRAINS, this.getMPInternalVariables(mpt.getLabel(), (IVector)this.getRepresentation().getConstitutiveVariables().get(this.GENERALIZED_STRAINS)));
	    IMatrix aux4 = mpt.getCm().mountC(mpt.getContinuousPointModel(), mpt.getMaterial(), mpt.getConstitutiveVariables());
	    aux4.setScale(mpt.getVolumeValue());
	    aux.add(aux4);
	}
	return aux;
    }

    /* (non-Javadoc)
     * @see br.ufmg.dees.insane.materialMedia.degeneration.Degeneration#mountCt()
     */
    public IMatrix mountCt() {
	IMatrix aux = new IMatrix(this.representation.getAnalysisModel().getNumberOfDualInternalVariables(),this.representation.getAnalysisModel().getNumberOfInternalVariables());
	aux.setZero();
	ListIterator mps = this.getMaterialPoints().listIterator();
	while(mps.hasNext()) {
	    MaterialPoint mpt = (MaterialPoint) mps.next();
	    OnePointConstModel opcm = (OnePointConstModel)mpt.getCm();
	    mpt.setConstitutiveVariables(opcm.COORDS, mpt.getPoint());
	    mpt.setConstitutiveVariables(opcm.STRAINS, this.getMPInternalVariables(mpt.getLabel(), (IVector)this.getRepresentation().getConstitutiveVariables().get(this.GENERALIZED_STRAINS)));
	    IMatrix aux4 = mpt.getCm().mountCt(mpt.getContinuousPointModel(), mpt.getMaterial(), mpt.getConstitutiveVariables());
	    aux4.setScale(mpt.getVolumeValue());
	    aux.add(aux4);
	}
	return aux;
    }

    /* (non-Javadoc)
     * @see br.ufmg.dees.insane.materialMedia.degeneration.Degeneration#mountCs()
     */
    public IMatrix mountCs() {
	IMatrix aux = new IMatrix(this.representation.getAnalysisModel().getNumberOfDualInternalVariables(),this.representation.getAnalysisModel().getNumberOfInternalVariables());
	aux.setZero();
	ListIterator mps = this.getMaterialPoints().listIterator();
	while(mps.hasNext()) {
	    MaterialPoint mpt = (MaterialPoint) mps.next();
	    OnePointConstModel opcm = (OnePointConstModel)mpt.getCm();
	    mpt.setConstitutiveVariables(opcm.COORDS, mpt.getPoint());
	    mpt.setConstitutiveVariables(opcm.STRAINS, this.getMPInternalVariables(mpt.getLabel(), (IVector)this.getRepresentation().getConstitutiveVariables().get(this.GENERALIZED_STRAINS)));
	    IMatrix aux4 = mpt.getCm().mountCs(mpt.getContinuousPointModel(), mpt.getMaterial(), mpt.getConstitutiveVariables());
	    aux4.setScale(mpt.getVolumeValue());
	    aux.add(aux4);
	}
	return aux;
    }

    /* (non-Javadoc)
     * @see br.ufmg.dees.insane.materialMedia.degeneration.Degeneration#mountDualInternalVariableVector(br.ufmg.dees.insane.util.IVector)
     */
    public IVector mountDualInternalVariableVector(IVector v) {
	this.getRepresentation().getConstitutiveVariables().put(this.GENERALIZED_STRAINS, v);
	IMatrix aux = this.mountCs();
	return aux.mul(v);
    }

    /* (non-Javadoc)
     * @see br.ufmg.dees.insane.materialMedia.degeneration.Degeneration#update()
     */
    public void update(){
	this.getRepresentation().getPreviousVariables().put(this.GENERALIZED_STRAINS, this.getRepresentation().getConstitutiveVariables().get(this.GENERALIZED_STRAINS));
	ListIterator mps = materialPoints.listIterator();
	while(mps.hasNext()) {
	    MaterialPoint mp = (MaterialPoint) mps.next();
	    mp.update();
	}
    }

    /* (non-Javadoc)
     * @see br.ufmg.dees.insane.materialMedia.degeneration.Degeneration#init()
     */
    public void init() {
	this.getRepresentation().setConstitutiveVariables(new HashMap<Object, Object>(1));
	this.getRepresentation().setPreviousVariables(new HashMap<Object, Object>(1));
	this.getRepresentation().getPreviousVariables().put(this.GENERALIZED_STRAINS, new IVector(this.getRepresentation().getAnalysisModel().getNumberOfInternalVariables()));
	this.getRepresentation().getConstitutiveVariables().put(this.GENERALIZED_STRAINS, new IVector(this.getRepresentation().getAnalysisModel().getNumberOfInternalVariables()));
	ListIterator mps = materialPoints.listIterator();
	while(mps.hasNext()) {
	    MaterialPoint mp = (MaterialPoint) mps.next();
	    mp.init();
	}
    }

}
